// BottomChatFrameWindow.cs
//
//  Copyright (C) 2008 Jacek Trubłajewicz
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//

using System;
using System.Drawing;
using Mono.Unix;

namespace wxGadu
{
	public class BottomChatFramePanel : wx.Panel
	{
		#region IDs
		private enum Id
		{
			sendButton = 700,
			moreButton,
			inputText,
			closeButton,
			clearMenu,
			searchMenu,
			enterSendMenu
		}
		#endregion
		
		#region controls
		private wx.Button sendButton;
		private wx.Button closeButton;
//		private wx.TextCtrl inputText;
		private MessageEntryTextCtrl inputText;
		private wx.Button moreButton;
		private wx.Menu moreMenu;
		private wx.MenuItem enterSendMenu;
		#endregion

		#region sizers
		private wx.BoxSizer topSizer;
		private wx.BoxSizer buttonSizer;
		#endregion
		
		#region data members
		private MainFrame mainFrame;
		private ChatFrame chatFrame;
		private bool enterSend;
		#endregion
		
		public BottomChatFramePanel(wx.Window parent):
			base(parent)
		{
			wx.SplitterWindow splitter = (wx.SplitterWindow) parent;
			chatFrame =  (ChatFrame) splitter.Parent;
			mainFrame = (MainFrame) chatFrame.MainFrame;
			enterSend = mainFrame.AppCfg.UserConf.EnterSend;
			
			DoControls();
			DoControlsProperties();
			DoLayout();
			
			EVT_MENU((int) Id.enterSendMenu, new wx.EventListener(OnEnterSendToggleClicked));
			EVT_BUTTON((int) Id.moreButton, new wx.EventListener(OnMoreButtonClicked));
			EVT_MENU((int) Id.clearMenu, new wx.EventListener(OnClearMenuClicked));
			EVT_TEXT_ENTER((int) Id.inputText, new wx.EventListener(OnEnterKey));
			EVT_BUTTON((int) Id.sendButton, new wx.EventListener(OnSendMessage));
			EVT_BUTTON((int) Id.closeButton, new wx.EventListener(OnCloseButtonClicked));
			EVT_UPDATE_UI((int) FakeId.showNotConnectedDialog, new wx.EventListener(ShowNotConnectedDialog));
		}

		private void DoControls()
		{
			sendButton = new wx.Button(this, (int) Id.sendButton, Catalog.GetString("Send"), 
			                           wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize,
			                           wx.Window.wxNO_BORDER);
			closeButton = new wx.Button(this, (int) Id.closeButton, Catalog.GetString("Close"), 
			                           wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize,
			                           wx.Window.wxNO_BORDER);
			moreButton = new wx.Button(this, (int) Id.moreButton, Catalog.GetString("More"), 
			                           wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize,
			                           wx.Window.wxNO_BORDER);			

		
			inputText = new MessageEntryTextCtrl(this, (int) Id.inputText, "", 
			                        wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize,
		                            wx.TextCtrl.wxTE_AUTO_SCROLL|
		                            wx.TextCtrl.wxTE_MULTILINE|
		                            wx.TextCtrl.wxTE_PROCESS_ENTER);
		}

		private void DoControlsProperties()
		{
			inputText.SetFocus();
			sendButton.ToolTip = Catalog.GetString("Click to send message");
			closeButton.ToolTip = Catalog.GetString("Close this window");
//			enterSendToggle.ToolTip = Catalog.GetString("Send message with Enter key");
//			clearButton.ToolTip = Catalog.GetString("Clear chat window");
			inputText.ToolTip = Catalog.GetString("Type your message here and press Send to send it");
			moreButton.ToolTip = Catalog.GetString("Additional options");
		}

		private void DoLayout()
		{
			topSizer = new wx.BoxSizer(wx.Orientation.wxVERTICAL);
			{
				topSizer.Add(inputText, 1, wx.Stretch.wxEXPAND|wx.Direction.wxBOTTOM, 5);

				buttonSizer = new wx.BoxSizer(wx.Orientation.wxHORIZONTAL);
				{
					buttonSizer.Add(sendButton, 0, wx.Direction.wxRIGHT, 5);
					buttonSizer.Add(5, 5, 1, 0, 0);
					buttonSizer.Add(moreButton, 0, wx.Direction.wxRIGHT, 5);
					buttonSizer.Add(closeButton, 0, 0, 0);
				}
				topSizer.Add(buttonSizer, 0, wx.Stretch.wxEXPAND);
			}
			
			topSizer.Fit(this);
			topSizer.SetSizeHints(this);
			this.Sizer = topSizer;
		}

		protected void OnEnterSendToggleClicked(object sender, wx.Event evt)
		{
			enterSend = enterSendMenu.Checked;			
		}
		
		protected void OnMoreButtonClicked(object sender, wx.Event evt)
		{
			if (moreMenu == null)
			{
				moreMenu = new wx.Menu();
				
				wx.MenuItem clear = new wx.MenuItem(moreMenu, (int) Id.clearMenu, Catalog.GetString("Clear chat window"));
				clear.Bitmap = mainFrame.ImageMgr[ImageManager.CLEAR];
//				wx.MenuItem search = new wx.MenuItem(moreMenu, (int) Id.searchMenu, Catalog.GetString("Search in catalog"));
//				search.Bitmap = mainFrame.ImageMgr[ImageManager.SEARCH];
				enterSendMenu = new wx.MenuItem(moreMenu, (int) Id.enterSendMenu, Catalog.GetString("'Enter' sends message"));
				enterSendMenu.Checkable = true;
				enterSendMenu.Checked = enterSend;
				//enterSendMenu.Bitmap = mainFrame.ImageMgr[ImageManager.ENTER];
				
				moreMenu.Append(clear);
//				moreMenu.Append(search);
				moreMenu.Append(enterSendMenu);
				enterSendMenu.Checked = enterSend;
			}
			
			this.PopupMenu(moreMenu);
		}
		
		private void OnClearMenuClicked(object sender, wx.Event evt)
		{
			chatFrame.ChatObject.ClearMessageList();
		}
		
		private void OnCloseButtonClicked(object sender, wx.Event evt)
		{
			chatFrame.Close();
		}
		
		private void ShowNotConnectedDialog(object sender, wx.Event evt)
		{
			wx.MessageDialog md = new wx.MessageDialog(this, Catalog.GetString("You must be connected to GG network to send messages"),
								Catalog.GetString("Not connected"), 
						        wx.MessageDialog.wxICON_ERROR|wx.MessageDialog.wxOK);
			md.ShowModal();
		}
		
		private void OnEnterKey(object sender, wx.Event evt)
		{
			if (enterSend)
				OnSendMessage(sender, evt);
			else
				evt.Skip();
		}
		
		private void OnSendMessage(object sender, wx.Event evt)
		{
			if (inputText.Value.Trim() == string.Empty)
				return;
			
			// check if user is still connected to GG network
			if (mainFrame.Dispatch.Gadu.IsGGLogged == false)
			{
				this.EventHandler.AddPendingEvent(new wx.UpdateUIEvent((int) FakeId.showNotConnectedDialog));
				return;
			}
			
			// push message forward 
			mainFrame.Dispatch.SendMessage(chatFrame.ChatObject.CallerUin, inputText.Value);
			
			// clear input field
			inputText.Value = string.Empty;
		}
	}
}
